import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_ncsjqz_getx/model/my_relese_entity.dart';
import 'package:flutter_ncsjqz_getx/page/square/details/guang_chang_details_view.dart';
import 'package:flutter_ncsjqz_getx/page_common/show_img_page.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/dialog_current_project_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:get/get.dart';

import 'history_release_logic.dart';

/// <summary>
/// author：zwb
/// dateTime： 2021/11/8 13:46
/// filePath： lib/page/my/history_relese/history_release_view.dart
/// desc: 历史发布
/// <summary>
///
class HistoryReleasePage extends StatefulWidget {
  List<MyReleseDataList>? myReleseDataList;
  int type; // 1我的  2他人
  HistoryReleasePage({this.myReleseDataList:const [],this.type:1});
  @override
  _HistoryReleasePageState createState() => _HistoryReleasePageState();
}

class _HistoryReleasePageState extends State<HistoryReleasePage> {
  final logic = Get.put(HistoryReleaseLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.myReleseDataList = widget.myReleseDataList;
    if(widget.myReleseDataList!.length == 0){
      logic.getData();
    }
  }
  @override
  Widget build(BuildContext context) {
    return GetBuilder<HistoryReleaseLogic>(
      builder: (controller) {
        return Scaffold(
          appBar: appBarWidget(title: "历史发布"),
          backgroundColor: Colors.white,
          body: baseScaffoldWidget(
            onRefresh: () async{
              if(widget.myReleseDataList==null){
                logic.getData();
              }
            },
            child: ListView(
              physics: NeverScrollableScrollPhysics(),
              padding: EdgeInsets.symmetric(vertical: 15),
              children: List.generate(logic.myReleseDataList!.length, (index){
                return InkWell(
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 0,horizontal: 15),
                    decoration: BoxDecoration(
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          width: 50,
                          child: InkWidget.title("${formatDate(DateTime.parse(logic.myReleseDataList![index].releaseTime!), [yyyy,])}"),
                        ),
                        SizedBox(
                          width: Get.width - 80,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(child: InkWidget.title("${logic.myReleseDataList![index].postTitle}",maxLines: 2),),
                                  Offstage(
                                    offstage: widget.type == 2,
                                    child: InkWidget.imgAsset("del.png",width: 20,onTap: (){
                                      showDialogGetx(
                                          subTitle: "删除后发布消息后不可恢复，是否确定删除",
                                          btnText: "确定删除",
                                          okTap: () async{
                                            await logic.del(logic.myReleseDataList![index].postId);
                                            await logic.getData();
                                            Get.back();
                                          }
                                      );
                                    }),
                                  ),
                                ],
                              ),
                              Container(
                                height:logic.myReleseDataList![index].postUrls!.length == 0 ?0: ( Get.width - 80) / 3,
                                padding: EdgeInsets.symmetric(vertical: 10),
                                child: ListView(
                                  shrinkWrap: true,
                                  scrollDirection: Axis.horizontal,
                                  children: List.generate(logic.myReleseDataList![index].postUrls!.length, (i){
                                    return InkWidget.imgNet("${logic.myReleseDataList![index].postUrls![i]}",onTap: (){
                                      Get.to(()=>ShowImg(imgs: logic.myReleseDataList![index].postUrls,));
                                    },width: ( Get.width - 80 -30) / 3,height: ( Get.width - 80 -30) / 3,borderRadius: BorderRadius.circular(10),padding: EdgeInsets.only(right: 15));
                                  }),
                                ),
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  InkWidget.text("${logic.myReleseDataList![index].releaseTime}",color: Colors.grey,),
                                  Row(
                                    children: [
                                      Icon(Icons.message,size: 18,color: Colors.grey,),
                                      InkWidget.text("${logic.myReleseDataList![index].comments!.length} 评论",color: Colors.grey,),
                                    ],
                                  ),
                                ],
                              ),
                              Divider(color: Colors.grey.shade400,height: 20,),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  onTap: (){
                    print("进入发布内容");
                    Get.to(()=>GuangChangDetailsPage(),arguments:logic.myReleseDataList![index].postId );
                  },
                );
              }),
            ),
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<HistoryReleaseLogic>();
    super.dispose();
  }
}